package cn.btks.backstagemanage.log.receiver;

import cn.btks.backstagemanage.log.pojo.SystemLog;
import cn.btks.backstagemanage.log.service.SystemLogService;
import cn.btks.commonserver.rabbitmq.RabbitConfig;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.util.SerializeObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author lijun
 * @Date 2021/1/4 20:11
 */
@Component
@RabbitListener(queues = RabbitConfig.QUEUE_LOG)
@Slf4j
public class RabbitLogReceiver {

    @Autowired
    SystemLogService systemLogService;

    @RabbitHandler
    public void process(Object object1){
        System.out.println("接收处理队列QUEUE_LOG当中的消息： " +object1);
        Message message = (Message) object1;
        byte[] body = message.getBody();
        Object object = SerializeObject.unserializeObj(body);
        cn.btks.commonserver.pojo.SystemLog systemLog1 = (cn.btks.commonserver.pojo.SystemLog )object;
        SystemLog systemLog2 = new SystemLog();
        if(systemLog1 != null){
            systemLog2.setId(systemLog1.getId());
            systemLog2.setLogMessage(systemLog1.getLogMessage());
            systemLog2.setOperationName(systemLog1.getOperationName());
            systemLog2.setCreatDateTime(systemLog1.getCreatDateTime());
            systemLog2.setRoleId(systemLog1.getRoleId());
            systemLog2.setRoleName(systemLog1.getRoleName());
            systemLog2.setUserId(systemLog1.getUserId());
            systemLog2.setUserName(systemLog1.getUserName());
            systemLog2.setObjMessage(systemLog1.getObjMessage());
            systemLog2.setModule(systemLog1.getModule());
            systemLog2.setUnitId(systemLog1.getUnitId());
            systemLog2.setUnitName(systemLog1.getUnitName());
            SystemLog systemLog3 = systemLogService.save(systemLog2);
            if(systemLog3 == null){
                log.error("保存MQ系统日志消息失败。");
            }
        }
    }
}
